



//
//  WaveLoadingView.m
//  界面动画集
//
//  Created by YYY on 2017/7/5.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "WaveLoadingView.h"

static CGFloat kWaveDuration = 5.0f;

@interface WaveLoadingView ()

@property(nonatomic,strong)UIImageView *duImageView;

@property(nonatomic,strong)UIImageView *grayImageView;

@property(nonatomic,strong)UIImageView *blueImageView;

@property(nonatomic,strong)CAShapeLayer *waveSinLayer;

@property(nonatomic,strong)CAShapeLayer *waveCosLayer;

@property(nonatomic,strong)CADisplayLink *displayLink;

@property(nonatomic,strong)CABasicAnimation *positionAnimation;

//波形参数
@property(nonatomic,assign)CGFloat waveWidth;
@property(nonatomic,assign)CGFloat waveHeight;
@property(nonatomic,assign)CGFloat waveMid;
//频率
@property(nonatomic,assign)CGFloat frequency;
@property(nonatomic,assign)CGFloat maxAmplitude;
@property(nonatomic,assign)CGFloat phase;
@property(nonatomic,assign)CGFloat phaseShift;

@end
@implementation WaveLoadingView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.waveWidth = CGRectGetWidth(self.frame);
        self.waveHeight = CGRectGetHeight(self.frame) * 0.5;
        self.waveMid = self.waveWidth/2.0f;
        self.frequency = 0.3;
        self.phaseShift = 8;
        self.maxAmplitude = self.waveHeight * 0.3;
        [self addSubview:self.duImageView];
        [self addSubview:self.grayImageView];
        [self addSubview:self.blueImageView];
        
        _grayImageView.layer.mask = self.waveCosLayer;
        _blueImageView.layer.mask = self.waveSinLayer;
    }
    return self;
}


-(CAShapeLayer *)waveSinLayer
{
    if (!_waveSinLayer)
    {
        _waveSinLayer = [CAShapeLayer layer];
        _waveSinLayer.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height);
    }
    return _waveSinLayer;
}

-(CAShapeLayer *)waveCosLayer
{
    if (!_waveSinLayer)
    {
        _waveCosLayer = [CAShapeLayer layer];
        _waveCosLayer.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height);
    }
    return _waveCosLayer;
}

-(UIImageView *)duImageView
{
    if (!_duImageView)
    {
        _duImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"du"]];
        _duImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }
    return _duImageView;
}

-(UIImageView *)grayImageView
{
    if (!_grayImageView)
    {
        _grayImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"gray"]];
        _grayImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }
    return _grayImageView;
}

-(UIImageView *)blueImageView
{
    if (!_blueImageView)
    {
        _blueImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"blue"]];
        _blueImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }
    return _blueImageView;
}

-(CABasicAnimation *)positionAnimation
{
    if (!_positionAnimation)
    {
        _positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        _positionAnimation.repeatCount = HUGE;
        _positionAnimation.removedOnCompletion = NO;
        _positionAnimation.duration = kWaveDuration;
        
    }
    return _positionAnimation;
}

//开始动画
-(void)startAnimation
{
    _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateWave)];
    [_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
    CGPoint position = _waveSinLayer.position;
    position.y = position.y - self.frame.size.height  - 10;
    self.positionAnimation.fromValue = [NSValue valueWithCGPoint:_waveSinLayer.position];
    _positionAnimation.toValue = [NSValue valueWithCGPoint:position];
    [_waveSinLayer addAnimation:_positionAnimation forKey:nil];
    [_waveCosLayer addAnimation:_positionAnimation forKey:nil];
}

//结束动画
-(void)stopAnimation
{
    [_displayLink invalidate];
    [_waveSinLayer removeAllAnimations];
    [_waveCosLayer removeAllAnimations];
    _waveSinLayer.path = nil;
    _waveCosLayer.path = nil;
    [self removeFromSuperview];
}

//更新波形
-(void)updateWave
{
    self.phase += self.phaseShift;
    _waveSinLayer.path = [self createWavePathWithType:0].CGPath;
    _waveCosLayer.path = [self createWavePathWithType:1].CGPath;
}

-(UIBezierPath *)createWavePathWithType:(NSInteger)type
{
    UIBezierPath *wavePath = [UIBezierPath bezierPath];
    CGFloat endX = 0;
    for (CGFloat x=0; x<self.waveWidth + 1; x+=1)
    {
        endX = x;
        CGFloat y = 0;
        if (type == 0)
        {
         
            y = self.maxAmplitude * sinf(360.0/_waveWidth *(x*M_PI/180)*self.frequency + self.phase*M_PI/180) + self.maxAmplitude;
        }
        else
        {
            y = self.maxAmplitude * cosf(360.0/_waveWidth *(x*M_PI/180)*self.frequency + self.phase*M_PI/180) + self.maxAmplitude;
        }
        if (x==0)
        {
            [wavePath moveToPoint:CGPointMake(x, y)];
        }
        else
        {
            [wavePath addLineToPoint:CGPointMake(x, y)];
        }
    }
    
    CGFloat endY = CGRectGetHeight(self.frame) + 10;
    [wavePath addLineToPoint:CGPointMake(endX, endY)];
    [wavePath addLineToPoint:CGPointMake(0, endY)];
    return wavePath;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
